home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 158 / 158.d81 / b.magic squares (.txt) < prev    next >
Commodore BASIC  |  2022-08-26  |  9KB  |  287 lines

  1. 2 poke56,56:clr:poke53371,0:poke788,49
  2. 4 poke53281,0:poke53280,0:print"[147]":ti$="000000":gosub60000
  3. 6 dv=peek(186):ifdv<8thendv=8
  4. 8 sys57812"magic sq font",dv,0:poke780,0:poke781,0:poke782,56:sys65493
  5. 10 fori=960to1023:readj:pokei,j:next
  6. 20 data255,252,0,128,4,0,128,4,0,128,4,0,128,4,0,128,4,0,128,4,0,128,4,0
  7. 30 data128,4,0,255,252,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  8. 40 data0,0,0,0,0,0,0,0,0,0
  9. 50 goto480
  10. 54 :
  11. 55 :rem   *** getkey routine ***
  12. 56 :
  13. 57 poke214,23:print:print"        [158][208]ress [211][208][193][195][197] [158]to continue":poke198,0
  14. 58 geta$:ifa$<>" "then58
  15. 59 return
  16. 62 :
  17. 63 :rem   *** clear half screen ***
  18. 64 :
  19. 65 fori=217to242:pokei,peek(i)or128:next
  20. 66 poke214,14:print
  21. 67 fori=0to8:printbl$:next:printbl$""
  22. 68 poke214,14:print
  23. 69 return
  24. 72 :
  25. 73 :rem   *** delay loop ***
  26. 74 :
  27. 75 fort=0to500:next:return
  28. 87 :
  29. 88 :rem   *** box routine ***
  30. 89 :
  31. 90 poke214,y:print:poke646,cb
  32. 100 printtab(x)tp$
  33. 110 forii=0to4
  34. 120 printtab(x)m0$:printtab(x)m1$
  35. 130 next
  36. 140 print"[145]"tab(x)bt$
  37. 150 return
  38. 160 :
  39. 170 :rem   *** fill routine ***
  40. 180 :
  41. 190 poke214,y-1:print:poke646,ct
  42. 200 fori=0to4:print""
  43. 210 forj=0to4
  44. 220 printtab(x+1+3*j)d$(i,j);
  45. 230 next:next:print""
  46. 240 return
  47. 250 :
  48. 260 :rem   *** solve routine ***
  49. 270 :
  50. 280 k=(fnr(5)+1)
  51. 290 fori=0to4:forj=0to4:d(i,j)=0:d$(i,j)="":next:next
  52. 300 fori=0to5:m(i)=0:next
  53. 310 fori=0to3
  54. 320 m=fnr(5)+1:ifm(m)or(m=k)then320
  55. 330 n(i)=m:m(m)=1:next
  56. 340 fori=0to4
  57. 350 forl=0toe(u,i)-1
  58. 360 d(i,l)=n(4-e(u,i)+l):next:p=0
  59. 370 forl=e(u,i)+1to4
  60. 380 d(i,l)=n(p):p=p+1:next
  61. 390 next
  62. 400 fori=0to4:d(i,e(u,i))=k:next
  63. 410 ifu=0thenfori=0to4:forj=0to4:f(i,j)=d(i,j):next:next
  64. 420 ifuthenfori=0to4:forj=0to4:d(i,j)=((d(i,j)-1)*5):next:next
  65. 430 fori=0to4:forj=0to4:d$(i,j)=right$(str$(d(i,j)),2):next:next
  66. 440 return
  67. 450 :
  68. 460 :rem    *** setup ***
  69. 470 :
  70. 480 poke53281,0:poke53280,0
  71. 490 x=rnd(-ti):def fnr(x)=int(rnd(1)*x)
  72. 500 tp$="[176][192][192][178][192][192][178][192][192][178][192][192][178][192][192][174]"
  73. 510 m0$="[221]  [221]  [221]  [221]  [221]  [221]"
  74. 520 m1$="[171][192][192][219][192][192][219][192][192][219][192][192][219][192][192][179]"
  75. 530 bt$="[173][192][192][177][192][192][177][192][192][177][192][192][177][192][192][189]"
  76. 535 bl$="                                       "
  77. 540 fori=0to4:reade(0,i):e(1,i)=4-e(0,i):next
  78. 550 data3,0,2,4,1
  79. 560 v=53248
  80. 570 fori=0to4:pokev+39+i,1:poke2040+i,15:next
  81. 580 pokev+28,0:pokev+29,31:pokev+23,31
  82. 590 fori=0to1:forj=0to4:readsp(i,j*2),sp(i,j*2+1):next:next
  83. 610 data114,68,42,84,90,100,138,116,66,132,226,68,42,84,250,100,202,116,18,132
  84. 620 fori=1to3:readw$(i):next
  85. 630 data"  [199][197][206][197][210][193][212][207][210]   ","   [212][213][212][207][210][201][193][204][160]  ","   [204][207][193][196][211][212][193][210]   "
  86. 638 ifti$<"000003"then638
  87. 640 :
  88. 650 :rem   *** menu ***
  89. 660 :
  90. 670 print"[147]":poke53272,31
  91. 680 print"           [159][212]he [205]ighty [156][204][207][193][196][211][212][193][210]"
  92. 690 print"       [159]5x5 [205]agic [211]quare [199]enerator"
  93. 700 print"         [155][193]lgorithm by [202]im [205]oran"
  94. 705 print"        [208]rogram by [203]nees [195]alhoon"
  95. 710 poke214,22:print:print"      [154][213]se [195][210][211][210] [213][208][154]/[196][207][215][206] [154]and [210][197][212][213][210][206]"
  96. 720 x=12:y=9:cb=7:gosub90
  97. 730 poke214,10
  98. 740 fori=0to9:printtab(13)"              ":next
  99. 750 poke214,12:print
  100. 760 fori=1to3:printtab(13)"[153]"w$(i):print:next:z=1
  101. 770 poke214,10+2*z:print:printtab(13)""w$(z)"[145]":poke198,0
  102. 780 geta$:ifa$<>"[145]"anda$<>""anda$<>chr$(13)then780
  103. 790 ifa$=chr$(13)then840
  104. 800 printtab(13)"[153]"w$(z)
  105. 810 ifa$="[145]"thenz=z-1:ifz<1thenz=3
  106. 820 ifa$=""thenz=z+1:ifz>3thenz=1
  107. 830 goto770
  108. 840 onzgoto880,2030,1260
  109. 850 :
  110. 860 :rem    *** draw screen ***
  111. 870 :
  112. 880 print"[147][150]         [205]agic [211]quare [199]enerator"
  113. 890 x=2:y=1:cb=12:gosub90:printtab(9)"[154][193]"
  114. 900 x=22:gosub90:printtab(30)"[154][194]"
  115. 910 x=12:y=12:cb=7:gosub90:printtab(14)"[154][205][193][199][201][195] [211][209][213][193][210][197]"
  116. 920 poke214,14:print
  117. 930 print"  [176][192][192][192][192][192][174]"tab(31)"[176][192][192][192][192][192][192][174]"
  118. 940 print"  [221][155][211][208][193][195][197][221]"tab(31)"[221][155][210][197][212][213][210][206][221]"
  119. 950 print"  [173][192][192][192][192][192][189]"tab(31)"[173][192][192][192][192][192][192][189]"
  120. 960 print"[153]  [193]nother"tab(33)"[205]enu"
  121. 970 print"  [211]quare"
  122. 980 :
  123. 990 :rem    *** fill boxes ***
  124. 1000 :
  125. 1010 fori=0to4:forj=0to4:f(i,j)=0:next:next
  126. 1020 u=0:gosub280
  127. 1030 x=2:y=1:ct=3:gosub190
  128. 1040 u=1:gosub280:x=22:gosub190
  129. 1050 fori=0to4:forj=0to4:d(i,j)=d(i,j)+f(i,j):next:next
  130. 1060 gosub430:y=12:x=12:ct=15:gosub190:pokev+21,0
  131. 1070 poke198,0
  132. 1080 geta$:ifa$<>" "anda$<>chr$(13)then1080
  133. 1090 ifa$=" "thengosub90:goto890
  134. 1120 goto670
  135. 1230 :
  136. 1240 :rem   *** loadstar ***
  137. 1250 :
  138. 1260 printtab(13)" [209]uit?  (y/n) [145]"
  139. 1270 geta$:ifa$<>"y"anda$<>"n"then1270
  140. 1280 ifa$="n"then770
  141. 1290 print"[147]"
  142. 1300 goto40000
  143. 2000 :
  144. 2010 :rem   *** tutorial ***
  145. 2020 :
  146. 2030 print"[147][129]    [200]ow [212]o [205]ake a 5 x 5 [205]agic [211]quare"
  147. 2040 print"[155]    [198]irst of all, let's define '[205]agic"
  148. 2050 print"[211]quare'. [201]t's a square matrix of"
  149. 2060 print"numbers whose columns, rows and"
  150. 2070 print"diagonals all add up to the same number."
  151. 2080 print"[145][212]his number is always the same for any"
  152. 2090 print"particular order of magic square. [212]he"
  153. 2100 print"formula for finding the number is"
  154. 2110 print"[159]                    (n * (n*n+1))
  155. 2120 [153]"          number =  tantantantantantantantantantantantantan
  156. 2130 print"                           2
  157. 2140 [153]"list(NULL)he number (or 'magic constant') of a"
  158. 2150 [153]"5 x 5 square is therefore 65.
  159. 2160 print"    [212]he following tutorial explains one"
  160. 2170 print"method (or algorithm) for generating up"
  161. 2180 print"to 14,400 different 5 x 5 magic squares."
  162. 2190 gosub57
  163. 2200 print"[147][155]    [212]his algorithm is called the [211][213][208][197][210]-"
  164. 2210 print"[199][197][207][205][197][212][210][201][195] [196][197][211][201][199][206] method and is taken"
  165. 2220 print"from [202]im [205]oran's '[212]he [215]onders of [205]agic"
  166. 2230 print"[211]quares' book. [212]he method was devised"
  167. 2240 print"by [202]im, himself."
  168. 2250 print"    [201]t involves creating two non-magical"
  169. 2260 print"[145]squares and adding them together to"
  170. 2270 print"make a third square that [201][211] magical."
  171. 2280 print"    [194]oth of the preliminary squares"
  172. 2290 print"are made the same way. [212]he difference"
  173. 2300 print"is that square [193] will contain the"
  174. 2310 print"numbers 1, 2, 3, 4 and 5, and [211]quare [194]"
  175. 2320 print"uses the numbers 0, 5, 10, 15 and 20."
  176. 2340 print"    [204]et's look at [211]quare [193]."
  177. 2350 gosub57
  178. 2360 print"[147]":
  179. 2370 x=2:y=1:cb=12:gosub90:printtab(9)"[154][193]"
  180. 2380 fori=0to4:pokev+i*2,sp(0,i*2):pokev+1+i*2,sp(0,1+i*2):next
  181. 2385 pokev+16,0:pokev+21,31
  182. 2390 poke214,15:print
  183. 2400 print"[155]    [212]he boxed squares represent [202]im's"
  184. 2410 print"[211][213][208][197][210]-[199][197][207][205][197][212][210][201][195] [196][197][211][201][199][206]. [210]emember this"
  185. 2420 print"design - it's magical.
  186. 2430 [153]"   (NULL)tart by choosing any number from"
  187. 2440 [153]"1 to 5. left$ow about 4? (NULL)ut it in the"
  188. 2450 [153]"boxed squares."
  189. 2460 [153]"open"[163]13)"4"
  190. 2462 [153]""[163]4)"4"
  191. 2464 [153]""[163]10)"4"
  192. 2466 [153]""[163]16)"4"
  193. 2468 [153]""[163]7)"4"
  194. 2470 [141]57:[141]65
  195. 2480 [153]"list    (NULL)ow, using the four other numbers,"
  196. 2490 [153]"1, 2, 3 and 5, arrange them in any"
  197. 2500 [153]"order. left$ow about 5-2-1-3?
  198. 2510 print"    [211]tarting with the square to the"
  199. 2520 print"right of a box, place these four"
  200. 2530 print"numbers [201][206] [211][197][209][213][197][206][195][197], left to right.  [201]f"
  201. 2540 print"you hit the end of the matrix, 'wrap"
  202. 2545 print"around' to the same row.":gosub75
  203. 2550 print"[159]"tab(4)"2"tab(7)"1"tab(10)"3"tab(13)""tab(16)"5":gosub75
  204. 2560 print""tab(4)""tab(7)"5"tab(10)"2"tab(13)"1"tab(16)"3":gosub75
  205. 2570 print""tab(4)"1"tab(7)"3"tab(10)""tab(13)"5"tab(16)"2":gosub75
  206. 2580 print""tab(4)"5"tab(7)"2"tab(10)"1"tab(13)"3"tab(16)"":g